//
//  CookingStyleViewController.m
//  FoodList
//
//  Created by lanou on 16/4/6.
//  Copyright © 2016年 王金晶. All rights reserved.
//

#import "CookingStyleViewController.h"
#import "CookingStyleTableViewCell.h"
#import "RequestManger.h"
#import "CookingStyleModel.h"
#import "FindSecondViewController.h"
@interface CookingStyleViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@property(nonatomic,strong)UITableView *tableView1;
@property(nonatomic,strong)UITableView *tableView2;
@property(nonatomic,strong)UITableView *tableView3;
@property(nonatomic,strong)UITableView *tableView4;
@property(nonatomic,strong)UITableView *tableView5;


@property (weak, nonatomic) IBOutlet UIButton *chuanButton;
@property (weak, nonatomic) IBOutlet UIButton *luButton;
@property (weak, nonatomic) IBOutlet UIButton *suButton;
@property (weak, nonatomic) IBOutlet UIButton *zheButton;
@property (weak, nonatomic) IBOutlet UIButton *yueButton;

@property(nonatomic,strong)NSMutableArray *modelArray;

@end

@implementation CookingStyleViewController

-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.alpha = 1;

}

#pragma mark ---------------------- 数据请求 --------------------------------

-(void)requestWithUrl:(NSString *)string {
    NSString *appdingStr = [NSString stringWithFormat:@"http://www.xdmeishi.com/index.php?m=mobile&c=index&a=getCookbooksByKeyword&keyword=%@",string];
    NSString *utfStr = [appdingStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [RequestManger requestWithURL:utfStr requestType:@"RequestGet" parDic:nil finish:^(NSData *data) {
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@",dic);
        NSArray *dataArray = dic[@"data"];
        _modelArray = [NSMutableArray array];
        for (NSDictionary *modelDic in dataArray) {
            CookingStyleModel *model = [[CookingStyleModel alloc]init];
            [model setValuesForKeysWithDictionary:modelDic];
            NSDictionary *referrerDic = modelDic[@"referrer"];
            model.nickname = referrerDic[@"nickname"];
            [_modelArray addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [_tableView1 reloadData];
            [_tableView2 reloadData];
            [_tableView3 reloadData];
            [_tableView4 reloadData];
            [_tableView5 reloadData];
        });
    } error:^(NSError *error) {
        NSLog(@"请求错误");
    }];
}

#pragma mark ----------------------- viewDidLoad ------------------------------
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"食谱分类";

    [self addView];
    
    // 一上来  川菜就是被选中状态，加载的页面就是川菜列表
    _chuanButton.backgroundColor = MyColour;
    [self requestWithUrl:_chuanButton.titleLabel.text];
}
// 在scrollView上加tableView
-(void)addView
{
    _scrollView.contentSize = CGSizeMake(Width * 5, Height - 114);
    _scrollView.delegate = self;
    
    _tableView1 = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, Width, Height - 114) style:UITableViewStylePlain];
    _tableView2 = [[UITableView alloc]initWithFrame:CGRectMake(Width, 0, Width, Height - 114) style:UITableViewStylePlain];
    _tableView3 = [[UITableView alloc]initWithFrame:CGRectMake(Width * 2, 0, Width, Height - 114) style:UITableViewStylePlain];
    _tableView4 = [[UITableView alloc]initWithFrame:CGRectMake(Width * 3, 0, Width, Height - 114) style:UITableViewStylePlain];
    _tableView5 = [[UITableView alloc]initWithFrame:CGRectMake(Width * 4, 0, Width, Height - 114) style:UITableViewStylePlain];
    
    _tableView1.rowHeight = 100;
    _tableView2.rowHeight = 100;
    _tableView3.rowHeight = 100;
    _tableView4.rowHeight = 100;
    _tableView5.rowHeight = 100;
    
    _tableView1.delegate = self;
    _tableView2.delegate = self;
    _tableView3.delegate = self;
    _tableView4.delegate = self;
    _tableView5.delegate = self;
    _tableView1.dataSource = self;
    _tableView2.dataSource = self;
    _tableView3.dataSource = self;
    _tableView4.dataSource = self;
    _tableView5.dataSource = self;
    
    [_scrollView addSubview:_tableView1];
    [_scrollView addSubview:_tableView2];
    [_scrollView addSubview:_tableView3];
    [_scrollView addSubview:_tableView4];
    [_scrollView addSubview:_tableView5];
}

#pragma mark ------------------ 五个菜系的触发事件 -----------------------------
- (IBAction)chuan:(id)sender {
    
    _scrollView.contentOffset = CGPointMake(0, 0);
    [self chuanButtonisSelected];
    [self requestWithUrl:_chuanButton.titleLabel.text];
}

- (IBAction)lu:(id)sender {
    _scrollView.contentOffset = CGPointMake(Width, 0);
    [self luButtonisSelected];
    [self requestWithUrl:_luButton.titleLabel.text];
}

- (IBAction)su:(id)sender {
    _scrollView.contentOffset = CGPointMake(Width * 2, 0);
    [self suButtonisSelected];
    [self requestWithUrl:_suButton.titleLabel.text];
}

- (IBAction)zhe:(id)sender {
    _scrollView.contentOffset = CGPointMake(Width * 3, 0);
    [self zheButtonisSelected];
    [self requestWithUrl:_zheButton.titleLabel.text];
}

- (IBAction)yue:(id)sender {
    _scrollView.contentOffset = CGPointMake(Width * 4, 0);
    [self yueButtonisSelected];
    [self requestWithUrl:_yueButton.titleLabel.text];
}

#pragma mark -------------------- button被选中时的状态 -----------------------
-(void)chuanButtonisSelected{
    _chuanButton.backgroundColor = MyColour;
    _luButton.backgroundColor = [UIColor clearColor];
    _suButton.backgroundColor = [UIColor clearColor];
    _zheButton.backgroundColor = [UIColor clearColor];
    _yueButton.backgroundColor = [UIColor clearColor];
}
-(void)luButtonisSelected{
    _chuanButton.backgroundColor = [UIColor clearColor];
    _luButton.backgroundColor = MyColour;
    _suButton.backgroundColor = [UIColor clearColor];
    _zheButton.backgroundColor = [UIColor clearColor];
    _yueButton.backgroundColor = [UIColor clearColor];
}
-(void)suButtonisSelected{
    _chuanButton.backgroundColor = [UIColor clearColor];
    _luButton.backgroundColor = [UIColor clearColor];
    _suButton.backgroundColor = MyColour;
    _zheButton.backgroundColor = [UIColor clearColor];
    _yueButton.backgroundColor = [UIColor clearColor];
}
-(void)zheButtonisSelected{
    _chuanButton.backgroundColor = [UIColor clearColor];
    _luButton.backgroundColor = [UIColor clearColor];
    _suButton.backgroundColor = [UIColor clearColor];
    _zheButton.backgroundColor = MyColour;
    _yueButton.backgroundColor = [UIColor clearColor];
}
-(void)yueButtonisSelected
{
    _chuanButton.backgroundColor = [UIColor clearColor];
    _luButton.backgroundColor = [UIColor clearColor];
    _suButton.backgroundColor = [UIColor clearColor];
    _zheButton.backgroundColor = [UIColor clearColor];
    _yueButton.backgroundColor = MyColour;
}
#pragma mark ------------------- scrollView结束减速的方法 ----------------
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = _scrollView.contentOffset.x / Width;
    if (index == 0) {
        [self chuanButtonisSelected];
        [self requestWithUrl:_chuanButton.titleLabel.text];
    }
    if (index == 1) {
        [self luButtonisSelected];
        [self requestWithUrl:_luButton.titleLabel.text];
    }
    if (index == 2) {
        [self suButtonisSelected];
        [self requestWithUrl:_suButton.titleLabel.text];
    }
    if (index == 3) {
        [self zheButtonisSelected];
        [self requestWithUrl:_zheButton.titleLabel.text];
    }
    if (index == 4) {
        [self yueButtonisSelected];
        [self requestWithUrl:_yueButton.titleLabel.text];
    }
}


#pragma mark -------------------- tableView -----------------------------
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _modelArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CookingStyleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cook"];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"CookingStyleTableViewCell" owner:nil options:nil] firstObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    CookingStyleModel *model = _modelArray[indexPath.row];
    [cell setModel:model];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FindSecondViewController *cookDetail = [[FindSecondViewController alloc]initWithNibName:@"FindSecondViewController" bundle:nil];
    CookingStyleModel *model = _modelArray[indexPath.row];
    cookDetail.myID = model.myID;
    cookDetail.name = model.name;
    cookDetail.imageURL = model.imageUrl;
    [self.navigationController pushViewController:cookDetail animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
